package com.letv.android.client.episode.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.android.client.adapter.LetvBaseAdapter;
import com.letv.core.bean.VideoBean;
import com.letv.core.constant.LetvConstant;
import com.letv.core.db.PlayRecordHandler;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.UIsUtils;

//import com.letv.download.bean.DownloadVideo;

public class DetailVideosGridAdapterPlayerLibs extends LetvBaseAdapter {

    private long curId;

    private boolean isDownload;

    private int curPosition = -1;

    private boolean isLandscape = false;

//    public HashMap<Long, DownloadVideo> downloadVideoHashMap;

    private Context mContext;

    public DetailVideosGridAdapterPlayerLibs(Context context) {
        super(context);     
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    @Override
    public int getCount() {
       return super.getCount();
    }

//    public int getGridItemHeight() {
//    	if (mCurGridItemHeight == 0) {
//    		mCurGridItemHeight = Math.min(UIsUtils.getScreenHeight(), UIsUtils.getScreenWidth()) * 11 / 80; // width / 5) * 11 / 16
//    	}
//    	return mCurGridItemHeight;
//    }

    public void setIsLandscape(boolean isLandscape) {
        this.isLandscape = isLandscape;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	VideoBean videoBean = (VideoBean) getItem(position);
        ViewHandler handler = null;
        if (convertView == null) {
            handler = new ViewHandler();
            if (isLandscape) {
                convertView = UIsUtils.inflate(mContext, R.layout.letv_player_gridview_item_playerlibs, parent, false);
                handler.checkImageView = (ImageView) convertView.findViewById(R.id.player_episode_item_check);
                handler.play_episode = (TextView) convertView.findViewById(R.id.player_episode_item_txt);
                handler.downloadStateTxt = (TextView) convertView.findViewById(R.id.local_tip);
            }
            else {
                convertView = UIsUtils.inflate(mContext, R.layout.letv_detailplay_half_videos_grid_item_playerlibs, parent, false);
                handler.play_layout = convertView.findViewById(R.id.videos_grid_item_play);
                handler.play_episode = (TextView) convertView.findViewById(R.id.videos_grid_item_play_episode);
                handler.downloadStateTxt = (TextView) convertView.findViewById(R.id.videos_grid_item_download_state_txt_play);
            }
            convertView.setTag(handler);
        } else {
            handler = (ViewHandler) convertView.getTag();
        }

        if (isLandscape) {
            if(LetvConstant.VIDEO_TYPE_KEY_ZHENG_PIAN.equals(videoBean.getVideoTypeKey())){
                handler.play_episode.setText(String.valueOf(videoBean.getEpisode()));
            }else{
                handler.play_episode.setText(String.valueOf(videoBean.getEpisode())+"预");
            }
//            DownloadVideo dbBean = null;
//            if (downloadVideoHashMap != null) {
//                dbBean = downloadVideoHashMap.get(videoBean.getId());
//            }
            if (isDownload) {
                handler.play_episode.setSelected(false);
//                if (dbBean != null) {
//                    if(dbBean.state == DownloadVideo.DownloadState.FINISHED_STATE){
//                        handler.checkImageView.setVisibility(View.GONE);
//                        handler.downloadStateTxt.setVisibility(View.VISIBLE);
//                    }else{
//                        handler.checkImageView.setVisibility(View.VISIBLE);
//                        handler.downloadStateTxt.setVisibility(View.GONE);
//                    }
//                } else {
                    handler.downloadStateTxt.setVisibility(View.GONE);
                    handler.checkImageView.setVisibility(View.GONE);
//                }

            } else {
                handler.checkImageView.setVisibility(View.GONE);
//                boolean hasDownload = null != dbBean && dbBean.state == DownloadVideo.DownloadState.FINISHED_STATE;
//                handler.downloadStateTxt.setVisibility(hasDownload ? View.VISIBLE : View.GONE);
                if (curId == videoBean.getId()) {
                    handler.play_episode.setSelected(true);
                    handler.play_episode.setBackgroundResource(R.drawable.letv_player_girdview_item_blue_selecter);
                    handler.play_episode.setTextColor(Color.WHITE);
                    handler.downloadStateTxt.setTextColor(Color.WHITE);
                } else {
                    handler.play_episode.setBackgroundResource(R.drawable.letv_player_girdview_item_selecter);
                    handler.play_episode.setSelected(false);
                    handler.play_episode.setTextColor(Color.WHITE);
                    handler.downloadStateTxt.setTextColor(mContext.getResources().getColor(R.color.letv_color_67b716));
                }
            }
        }
        else {
            /*
             * 180001:正片  180002：预告片 180003：花絮
             */
            if(videoBean.getVideoTypeKey().equals(LetvConstant.VIDEO_TYPE_KEY_ZHENG_PIAN)){
            	handler.play_episode.setText(String.valueOf(videoBean.getEpisode()));
            }else{
            	if (!TextUtils.isEmpty(videoBean.getEpisode())) {
            		handler.play_episode.setText(String.valueOf(videoBean.getEpisode())+"预");
            	}
            	else {
            		handler.play_episode.setText(String.valueOf(videoBean.getPorder())+"预");
            	}
            }

//            DownloadVideo dbBean = null;
//            if (downloadVideoHashMap != null) {
//                dbBean = downloadVideoHashMap.get(videoBean.getId());
//            }
//            if (dbBean != null) {
//                if (dbBean.state == DownloadVideo.DownloadState.FINISHED_STATE) {// 完成
//                    handler.play_layout.setBackgroundResource(R.color.letv_color_ffffffff);
//                    handler.downloadStateTxt.setVisibility(View.VISIBLE);
////                    handler.downloadStateTxt.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff67b716));
//                    handler.downloadStateTxt.setText(R.string.download_tip);
//                } else {// 未完成
////                    handler.play_layout.setBackgroundResource(R.color.letv_color_e3edfa);
//                    handler.downloadStateTxt.setVisibility(View.GONE);
//                }
//            } else {
                handler.play_layout.setBackgroundResource(R.color.letv_color_ffffffff);
                handler.downloadStateTxt.setVisibility(View.GONE);
//            }

            //当前播放中
            if (curId == videoBean.getId()) {
                handler.play_layout.setBackgroundResource(R.color.letv_color_ff00a0e9);
            	curPosition = position;
                handler.play_episode.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
                handler.downloadStateTxt.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffffffff));
            } else {
            	PlayRecordHandler playRecordHandler = new PlayRecordHandler(mContext);
                boolean isWatched = playRecordHandler.queryPlayTraceByWatchedStatus(videoBean.getId());
                if (isWatched) {
                	LogInfo.log("hong", "isWatched: " + isWatched);
                	handler.play_episode.setTextColor(mContext.getResources().getColor(R.color.letv_color_ffcccccc));
                }
                else {
                	handler.play_episode.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff444444));
                }
                handler.downloadStateTxt.setTextColor(mContext.getResources().getColor(R.color.letv_color_ff67b716));
            }
        }
        
        return convertView;
    }

    private class ViewHandler {
        private View play_layout;
        private ImageView checkImageView;
        private TextView play_episode;
        private TextView downloadStateTxt;
    }

    public void setCurId(long curId) {
        this.curId = curId;
    }

    public void setDownload(boolean isDownload) {
        this.isDownload = isDownload;
    }

    public int getCurPosition() {
		return curPosition;
	}

	public void setCurPosition(int curPosition) {
		this.curPosition = curPosition;
	}
}
